﻿using CefSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dianping_shop_comment_collecter
{
    public class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory
    {
        public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {

            var uri = new Uri(request.Url);
            var fileName = uri.AbsolutePath; 
            if (uri.Host == "www.dianping.com" && schemeName == "http" &&  uri.AbsolutePath.Contains("ajax/json/shopDynamic/allReview") )
            {
                return new CefSharpSchemeHandler();
            }

            return null;
        }
    }


    internal class CefSharpSchemeHandler: ResourceHandler
    {


        public override Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl)
        {

           var dataStream = base.GetResponse(response, out responseLength, out redirectUrl); 

            return dataStream;
        }

        public override bool ProcessRequestAsync(IRequest request, ICallback callback)
        {
            var uri = new Uri(request.Url);
            var fileName = uri.AbsolutePath;
 
            System.Diagnostics.Debug.WriteLine(" CefSharpSchemeHandler ProcessRequestAsync" +  request.Url);

            return base.ProcessRequestAsync(request, callback);
        }
    }
}
